package com.ztjy.repository.controller;


import com.github.pagehelper.PageInfo;
import com.ztjy.bean.*;
import com.ztjy.purchase.biz.CDetailsBiz;
import com.ztjy.purchase.biz.CProductBiz;
import com.ztjy.purchase.biz.CPurchaseBiz;
import com.ztjy.repository.biz.KcWarehouseBiz;
import com.ztjy.repository.biz.RkWarehouseBiz;
import com.ztjy.repository.biz.WarehouseBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rep/rkware")
@SessionAttributes({"purchase","warehouses"})
public class RkWarehouseController {
    @Autowired
    private RkWarehouseBiz rkbiz;
    @Autowired
    private CPurchaseBiz pbiz;
    @Autowired
    private CDetailsBiz cdbiz;
    @Autowired
    private WarehouseBiz wbiz;
   @Autowired
   private CProductBiz prbiz;
   @Autowired
   private KcWarehouseBiz kcbiz;

    @RequestMapping("/allrkware")
    public String allCkWare(Warehouse warehouse, CPurchase purchasee,Model model, String wareName2, String rkIndent, String state, @RequestParam(defaultValue = "1", value = "pageno") Integer pageNo, HttpSession session){
        Integer pageSize=6;
        Users user=(Users) session.getAttribute("currUser");
        Integer jobId = user.getJobId();
        Map map=new HashMap();
        map.put("1","未入库");
        map.put("2","已入库");
        if (jobId==6 || jobId==1 ||jobId==12){
            Warehouse w=new Warehouse();
            w.setCName(wareName2);
            RkWarehouse rk=new RkWarehouse();
            rk.setRkIndent(rkIndent);
            rk.setState(state);
            rk.setWarehouse(w);
            PageInfo<RkWarehouse> allRk = rkbiz.findAllRkWare(rk, pageNo, pageSize);
            model.addAttribute("wareName2",wareName2);
            model.addAttribute("rkIndent",rkIndent);
            model.addAttribute("state",state);
            model.addAttribute("rkinfo",allRk);
            model.addAttribute("rkmap",map);
            return "forward:/storage/stock/stockList.html";
        }else if(jobId==7||jobId==8){
            Warehouse w=new Warehouse();
            w.setCName(wareName2);
            RkWarehouse rk=new RkWarehouse();
            rk.setRkIndent(rkIndent);
            rk.setState(state);
            rk.setWarehouse(w);
            rk.setUserId(user.getuId());
            PageInfo<RkWarehouse> allRk = rkbiz.findAllRkWare(rk, pageNo, pageSize);
            List<CPurchase> purchase = pbiz.findAllCPurchase(purchasee);
            model.addAttribute("purchase",purchase);
            model.addAttribute("wareName2",wareName2);
            model.addAttribute("rkIndent",rkIndent);
            model.addAttribute("state",state);
            model.addAttribute("rkinfo",allRk);
            model.addAttribute("map",map);
            return "forward:/storage/stock/stockList.html";
        }else{
            return "";
        }
    }

    /**
     * 查看详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/CKdetails/{id}")
    public String CK(@PathVariable("id")Integer id, Model model){
        RkWarehouse rkWarehouseById = rkbiz.findRkWarehouseById(id);
        CPurchase purchase = pbiz.findCPurchaseByPid(rkWarehouseById.getRkIndent());
        List<CDetails> details = cdbiz.findByIdCDetails(rkWarehouseById.getRkIndent());
        model.addAttribute("rk",rkWarehouseById);
        model.addAttribute("pur",purchase);
        model.addAttribute("cde",details);
        return "forward:/storage/stock/stockView.html";
    }

    /**
     * 跳转添加页面
     * @param rkWarehouse
     * @param purchasee
     * @param warehouse
     * @param model
     * @return
     */
    @RequestMapping("/doAdd")
    public String doAdd(RkWarehouse rkWarehouse,CPurchase purchasee,Warehouse warehouse,Model model){
        purchasee.setCgState(1);
        List<CPurchase> purchase = pbiz.findNoCK(purchasee);
        List<Warehouse> warehouses = wbiz.findAllWarehouse(warehouse);
        warehouse.setState("1");
        model.addAttribute("warehouses",warehouses);
        model.addAttribute("purchase",purchase);
        List<RkWarehouse> rkWarehouses = rkbiz.findAllRkWarehouse(rkWarehouse);
        model.addAttribute("rkW",rkWarehouse);
        return "forward:/storage/stock/stockAdd.html";
    }


    /**
     * 添加方法
     * @param rkWarehouse
     * @return
     */
   @RequestMapping("/RHadd")
    public String add(RkWarehouse rkWarehouse){
        rkWarehouse.setState("1");
        boolean flag = rkbiz.AddRkWarehouse(rkWarehouse);
        if(flag){
            return "redirect:/rep/rkware/allrkware";
        }else{
            return "forward:/storage/stock/stockAdd.html";
        }
    }

    /**
     * 状态修改
     * @param id
     * @return
     */
    @RequestMapping("/updatestate")
    public String updateState(Integer id) {
        RkWarehouse rkw = rkbiz.findRkWarehouseById(id);
        if (rkw.getState().equals("1")) {
            rkw.setState("2");
            List<CDetails> cDetails = cdbiz.findByIdCDetails(rkw.getRkIndent());
            for (CDetails c : cDetails) {
                KcWarehouse kc1 = new KcWarehouse();
                    kc1.setProductId(c.getProductId());
                    kc1.setTypeId(c.getProducts().getTypeId());
                    kc1.setBrandId(c.getProducts().getTypes().getBrandId());
                    kc1.setFactoryId(c.getProducts().getFirmId());
                    kc1.setWarehouseId(rkw.getWarehouseId());
                    KcWarehouse kc = kcbiz.findAll(kc1);
                if(kc==null){
                    kc1=new KcWarehouse();
                    kc1 .setProductId(c.getProductId());
                    kc1 .setTypeId(c.getProducts().getTypeId());
                    kc1 .setBrandId(c.getProducts().getTypes().getBrandId());
                    kc1 .setFactoryId(c.getProducts().getFirmId());
                    kc1 .setWarehouseId(rkw.getWarehouseId());
                    kc1 .setRepertory(c.getCount());
                    kcbiz.addKC(kc1 );
                }else{
                    kc1.setRepertory(c.getCount());
                    kcbiz.updateRK(kc1, 1);
                }
            }
        } else {
            rkw.setState("1");
            List<CDetails> cDetails = cdbiz.findByIdCDetails(rkw.getRkIndent());
            for (CDetails c : cDetails) {
                KcWarehouse kc1 = new KcWarehouse();
                kc1.setProductId(c.getProductId());
                kc1.setTypeId(c.getProducts().getTypeId());
                kc1.setBrandId(c.getProducts().getTypes().getBrandId());
                kc1.setFactoryId(c.getProducts().getFirmId());
                kc1.setWarehouseId(rkw.getWarehouseId());
                KcWarehouse kc = kcbiz.findAll(kc1);
                if(kc.getRepertory()==c.getCount()){
                    kc1 = new KcWarehouse();
                    kc1.setProductId(c.getProductId());
                    kc1.setTypeId(c.getProducts().getTypeId());
                    kc1.setBrandId(c.getProducts().getTypes().getBrandId());
                    kc1.setFactoryId(c.getProducts().getFirmId());
                    kc1.setWarehouseId(rkw.getWarehouseId());
                    kcbiz.delKC(kc1);
                }else{
                    kc1.setRepertory(c.getCount());
                    kcbiz.updateRK(kc1,2);
                }
            }
        }
        rkbiz.UpdateRkWarehouse(rkw);
        return "redirect:/rep/rkware/allrkware";
    }

}
